﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Entity = SoTaiChinh.Library.Entity;
using Business = SoTaiChinh.Library.Business;
using Common = SoTaiChinh.Library.Common;

public partial class templates_client_usercontrols_menu : System.Web.UI.UserControl
{
    Common.Algorithm.Validators validator = new Common.Algorithm.Validators();
    DataTable dsDanhMucCha = new DataTable();
    DataTable dsDanhMucCon1 = new DataTable();
    Business.DanhMuc business = new Business.DanhMuc();
    Entity.DanhMuc entityDanhMucCha = new Entity.DanhMuc();
    Entity.DanhMuc entityDanhMucCon1 = new Entity.DanhMuc();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDanhMuc();                        
        }
    }
    protected void LoadDanhMuc()
    {
        try
        {
            string str = " ";
            entityDanhMucCha.DanhMucCha = 0;
            dsDanhMucCha = business.SelectByDanhMucCha(entityDanhMucCha);            
            if (dsDanhMucCha.Rows.Count > 0)
            {
                str += "<ul id=\"menu_main\" class=\"sf-menu sf-navbar\" style=\" width:1000px; margin-top:165px; margin-left:15px\">";
                for (int i = 0; i < dsDanhMucCha.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dsDanhMucCha.Rows[i]["Tren"].ToString()) == true && Convert.ToBoolean(dsDanhMucCha.Rows[i]["TrangThai"].ToString()) == true)
                    {
                        entityDanhMucCon1.DanhMucCha = Convert.ToInt32(dsDanhMucCha.Rows[i]["Id"].ToString());
                        dsDanhMucCon1 = business.SelectByDanhMucCha(entityDanhMucCon1);

                        string link = "";
                        string css = "";
                        if (dsDanhMucCha.Rows[i]["Ten"].ToString() == "Trang chủ")
                        {
                            link = "../pages/";
                        }
                        else
                        {
                            link = "list-news.aspx?action=cate&chuyenmuc=" + dsDanhMucCha.Rows[i]["Id"].ToString();
                        }
                        //Begin set css active cho menu danh muc duoc chon
                        // Tách chuỗi url và kiểm tra xem đang ở trang chủ hay là  trang con
                        string[] url =Request.Url.LocalPath.ToString().Split('/');
                        string[] chuoiurl = Request.Url.ToString().Split('=');

                        if (url.Length == 4 && url[3] == "" || url[3] == "default.aspx" || chuoiurl[1] == "timkiem&chuyenmuc" || chuoiurl[2] == "61" || chuoiurl[1] == "view&chude") //Nếu như đang ở trang chủ
                        {
                            // in ra menu cấp 1 với class active mặc định cho trang chủ
                            str += "<li class=\"current active\"><a class=\"sf-with-ul\" href=\"" + link + "\">" + dsDanhMucCha.Rows[i]["Ten"].ToString() + "<span class=\"sf-sub-indicator\"> &#187;</span></a>";                                                        
                        }
                        else // Ngược lại nếu đang ở trang con
                        {
                            for (int k = 0; k < dsDanhMucCon1.Rows.Count; k++)
                            {
                                if (dsDanhMucCon1.Rows[k]["Id"].ToString() == chuoiurl[2] || dsDanhMucCha.Rows[i]["Id"].ToString() == chuoiurl[2]) // Nếu Id của danh mục đang xem trùng với id danh mục trên URL thì set css active cho danh mục đó
                                {
                                    css = " current active";
                                }
                            }
                            //In menu cấp 1 với css active tương ứng
                            Business.DanhMuc bkt = new Business.DanhMuc();
                            Entity.DanhMuc ekt = new Entity.DanhMuc();
                            ekt.Id = Convert.ToInt32(chuoiurl[2]);
                            DataTable dt = bkt.SelectPrimaryKey(ekt);
                            if (Convert.ToBoolean(dt.Rows[0]["Tren"].ToString()) == true)
                            {
                                str += "<li class=\"" + css + "\"><a class=\"sf-with-ul\" href=\"" + link + "\">" + dsDanhMucCha.Rows[i]["Ten"].ToString() + "<span class=\"sf-sub-indicator\"> &#187;</span></a>";
                            }
                            else
                            {
                                str += "<li class=\"current active\"><a class=\"sf-with-ul\" href=\"" + link + "\">" + dsDanhMucCha.Rows[i]["Ten"].ToString() + "<span class=\"sf-sub-indicator\"> &#187;</span></a>";
                            }
                            
                        }
                        //End set css cho menu danh muc duoc chon
                        if (dsDanhMucCon1.Rows.Count==0) //Nếu là trang chủ thì in cứng menu cấp 2 cho danh mục trang chủ
                        {
                            str += "<ul class=\"ul\">";

                            str += "<li class=\"sub\">";
                            str += "<img src=\"../templates/client/images/icon-home.png\" alt=\"home\" />";
                            str += "<a href=\"../pages/\" style=\"margin-top:12px; margin-left:25px;\">Sở tài chính Gia Lai</a>";
                            str += "</li>";

                            str += "<li class=\"sub\">";
                            str += "<img src=\"../templates/client/images/map.png\" alt=\"home\" />";
                            str += "<a href=\"#\" style=\"margin-top:12px; margin-left:25px;\">Địa chỉ: Pleiku - Gia Lai</a>";
                            str += "</li>";

                            str += "<li class=\"sub\">";
                            str += "<img src=\"../templates/client/images/phone.png\" alt=\"home\" />";
                            str += "<a href=\"#\" style=\"margin-top:12px; margin-left:25px;\">Điện thoại: (059) 3 852 000</a>";
                            str += "</li>";

                            str += "<li class=\"sub\">";
                            str += "<img src=\"../templates/client/images/fax.png\" alt=\"home\" />";
                            str += "<a href=\"#\" style=\"margin-top:12px; margin-left:25px;\">Fax: (059) 3 852 000</a>";
                            str += "</li>";

                            str += "<li class=\"sub\">";
                            str += "<img src=\"../templates/client/images/email.png\" alt=\"home\" />";
                            str += "<a href=\"#\" style=\"margin-top:12px; margin-left:25px;\">Email: stcgialai@gov.vn</a>";
                            str += "</li>";

                            str += "</ul>";
                        }
                        else
                        {
                            // Load menu cấp 2                            
                            if (dsDanhMucCon1.Rows.Count > 0)
                            {
                                str += "<ul class=\"ul\"  style =\"margin-top:-4px\">";
                                for (int j = 0; j < dsDanhMucCon1.Rows.Count; j++)
                                {
                                    if (Convert.ToBoolean(dsDanhMucCon1.Rows[j]["Tren"].ToString()) == true && Convert.ToBoolean(dsDanhMucCon1.Rows[j]["TrangThai"].ToString()) == true)
                                    {
                                        str += "<li class=\"sub\">";
                                        str += "<img src=\"../templates/client/images/row_3.gif\" style=\"margin:15px 5px 0 15px;\"/>";
                                        str += "<a class=\"sf-with-ul\"  href=\"list-news.aspx?action=sub&chuyenmuc=" + dsDanhMucCon1.Rows[j]["Id"].ToString() + "\">" + dsDanhMucCon1.Rows[j]["Ten"].ToString() + "</a>";
                                        str += "</li>";
                                    }
                                }
                                str += "</ul>";
                            }
                        }
                        str += "</li>";
                    }
                }
                str += "</ul>";
            }
            menu.InnerHtml = str;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
